package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Fillertype;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FillertypeController")
public class FillertypeController extends CmiEmaController {

	@RequestMapping("/listFillertypes")
	public ModelAndView listFillertypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = fillertypePaginationService.updatePagination(request, pagination);
		Set<Fillertype> fillertypes = fillertypePaginationService.getPaginationFillertypes(pagination);

		mav.addObject("fillertypes", fillertypes);
		mav.addObject("entityName", "fillertype");
		mav.addObject("pagination", pagination);
		mav.setViewName("fillertype/listFillertypes");
		return mav;
	}

	public String listFillertypes() {
		return "redirect:/listFillertypes";
	}

	@RequestMapping("/viewFillertype")
	public ModelAndView viewFillertype(@RequestParam Integer fillertypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("fillertype", fillertypeDAO.findFillertypeByPrimaryKey(fillertypeId));
		mav.setViewName("fillertype/viewFillertype");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFillertypes")
	public ModelAndView confirmDeleteFillertypes(
			@RequestParam(value = "deleteFillertypesIds", required = true) Set<Integer> deleteFillertypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Fillertype> deleteFillertypes = fillertypeDAO.findFillertypesByIds(deleteFillertypesIds);
		Set<Fillertype> referencedFillertypes = fillertypeService.loadReferenceFillertypes(deleteFillertypes);
		boolean hasReferenced;
		if (referencedFillertypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteFillertypes);
			hasReferenced = false;
		} else {
			deleteFillertypes.removeAll(referencedFillertypes);
			mav.addObject("referencedEntitys", referencedFillertypes);
			mav.addObject("deleteEntitys", deleteFillertypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Fillertype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFillertypes");
		return mav;
	}

	@RequestMapping("/deleteFillertype")
	public String deleteFillertype(@RequestParam Integer fillertypeId) {
		Fillertype fillertype = fillertypeDAO.findFillertypeByPrimaryKey(fillertypeId);
		fillertypeService.deleteFillertype(fillertype);
		return "forward:/listFillertypes";
	}

	@RequestMapping("/confirmDeleteFillertype")
	public ModelAndView confirmDeleteFillertype(@RequestParam Integer fillertypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("fillertype", fillertypeDAO.findFillertypeByPrimaryKey(fillertypeId));

		mav.setViewName("fillertype/deleteFillertype");

		return mav;
	}

	@RequestMapping("/deleteFillertypes")
	public String deleteFillertypes(
			@RequestParam(value = "deleteFillertypesId", required = true) Set<Integer> deleteFillertypesId) {
		fillertypeService.deleteFillertypes(deleteFillertypesId);
		return "forward:/listFillertypes";
	}

	@RequestMapping("/editFillertype")
	public ModelAndView editFillertype(@RequestParam Integer fillertypeId) {

		ModelAndView mav = new ModelAndView();

		Fillertype fillertype = fillertypeDAO.findFillertypeByPrimaryKey(fillertypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("fillertype", fillertype);

		// 把所有的引用fillertype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(fillertypeService.loadFillertypeObjectReferencedSet(fillertype));

		// 将编辑页面fillertype添加到mav中
		mav.setViewName("fillertype/editFillertype");
		return mav;
	}

	@RequestMapping("/newFillertype")
	public ModelAndView newFillertype() {
		ModelAndView mav = new ModelAndView();
		Fillertype fillertype = new Fillertype();

		mav.addObject("fillertype", fillertype);

		// 把所有的引用fillertype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(fillertypeService.loadFillertypeObjectReferencedSet(fillertype));

		mav.addObject("newFlag", true);
		mav.setViewName("fillertype/editFillertype");

		return mav;
	}

	@RequestMapping("/saveFillertype")
	public ModelAndView saveFillertype(@ModelAttribute Fillertype fillertype) {

		ModelAndView mav = new ModelAndView();

		Fillertype existingFillertype = CmiHibernateProxyUtil.deproxy(
				fillertypeDAO.findFillertypeByPhysicalPrimaryKey(fillertype.getName(), fillertype.getParent()));

		// 新建记录
		if (fillertype.getId() == null) {
			if (fillertype.equals(existingFillertype)) {

				mav.addAllObjects(fillertypeService.loadFillertypeClassReferencedSet(fillertype));
				// mav = editContinue(mav, fillertype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("fillertype/editFillertype");
			} else {
				fillertypeService.saveFillertype(fillertype);
				mav.setViewName("forward:/listFillertypes");
			}
		}
		// 编辑记录
		else {
			Fillertype originFillertype = CmiHibernateProxyUtil
					.deproxy(fillertypeDAO.findFillertypeByPrimaryKey(fillertype.getId()));
			if (!fillertype.equals(originFillertype) && fillertype.equals(existingFillertype)) {
				mav.addAllObjects(fillertypeService.loadFillertypeClassReferencedSet(fillertype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("fillertype/editFillertype");
			} else {
				fillertypeService.saveFillertype(fillertype);
				mav.setViewName("forward:/listFillertypes");
			}
		}
		return mav;
	}

	@RequestMapping("/fillertypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
